// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.erosion.categories;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import zombie.erosion.ErosionData;
import zombie.erosion.obj.ErosionObjOverlay;
import zombie.erosion.obj.ErosionObjOverlaySprites;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoObject;
import zombie.iso.IsoWorld;

public final class WallCracks extends ErosionCategory {
    private ArrayList<ErosionObjOverlay> objs = new ArrayList();
    private static final int DIRNW = 0;
    private static final int DIRN = 1;
    private static final int DIRW = 2;
    private ArrayList<ArrayList<Integer>> objsRef = new ArrayList();
    private ArrayList<ArrayList<Integer>> botRef = new ArrayList();
    private ArrayList<ArrayList<Integer>> topRef = new ArrayList();
    private int[] spawnChance = new int[100];

    @Override
    public boolean replaceExistingObject(
        IsoGridSquare _sq, ErosionData.Square _sqErosionData, ErosionData.Chunk _chunkData, boolean _isExterior, boolean _hasWall
    ) {
        return false;
    }

    @Override
    public boolean validateSpawn(
        IsoGridSquare _sq, ErosionData.Square _sqErosionData, ErosionData.Chunk _chunkData, boolean _isExterior, boolean _hasWall, boolean _isRespawn
    ) {
        if (!_isExterior) {
            return false;
        } else {
            int int0 = _sqErosionData.noiseMainInt;
            int int1 = this.spawnChance[int0];
            if (int1 == 0) {
                return false;
            } else if (_sqErosionData.rand(_sq.x, _sq.y, 101) >= int1) {
                return false;
            } else {
                byte _byte = -1;
                IsoObject isoObject0 = this.validWall(_sq, true, false);
                if (isoObject0 != null) {
                    String string0 = isoObject0.getSprite().getName();
                    if (string0 != null && string0.startsWith("fencing")) {
                        isoObject0 = null;
                    }
                }

                IsoObject isoObject1 = this.validWall(_sq, false, false);
                if (isoObject1 != null) {
                    String string1 = isoObject1.getSprite().getName();
                    if (string1 != null && string1.startsWith("fencing")) {
                        isoObject1 = null;
                    }
                }

                if (isoObject0 != null && isoObject1 != null) {
                    _byte = 0;
                } else if (isoObject0 != null) {
                    _byte = 1;
                } else {
                    if (isoObject1 == null) {
                        return false;
                    }

                    _byte = 2;
                }

                boolean _boolean = int0 < 35 && _sqErosionData.magicNum > 0.3F;
                WallCracks.CategoryData categoryData0 = (WallCracks.CategoryData)this.setCatModData(_sqErosionData);
                categoryData0.gameObj = (Integer)((ArrayList)this.objsRef.get(_byte))
                    .get(_sqErosionData.rand(_sq.x, _sq.y, ((ArrayList)this.objsRef.get(_byte)).size()));
                categoryData0.alpha = 0.0F;
                categoryData0.spawnTime = int0;
                if (_boolean) {
                    IsoGridSquare isoGridSquare = IsoWorld.instance.CurrentCell.getGridSquare(_sq.getX(), _sq.getY(), _sq.getZ() + 1);
                    if (isoGridSquare != null) {
                        IsoObject isoObject2 = this.validWall(isoGridSquare, _byte == 1, false);
                        if (isoObject2 != null) {
                            int int2 = _sqErosionData.rand(_sq.x, _sq.y, ((ArrayList)this.botRef.get(_byte)).size());
                            categoryData0.gameObj = (Integer)((ArrayList)this.botRef.get(_byte)).get(int2);
                            WallCracks.CategoryData categoryData1 = new WallCracks.CategoryData();
                            categoryData1.gameObj = (Integer)((ArrayList)this.topRef.get(_byte)).get(int2);
                            categoryData1.alpha = 0.0F;
                            categoryData1.spawnTime = categoryData0.spawnTime;
                            categoryData0.hasTop = categoryData1;
                        }
                    }
                }

                return true;
            }
        }
    }

    @Override
    public void update(IsoGridSquare _sq, ErosionData.Square _sqErosionData, ErosionCategory.Data _sqCategoryData, ErosionData.Chunk _chunkData, int _eTick) {
        WallCracks.CategoryData categoryData = (WallCracks.CategoryData)_sqCategoryData;
        if (_eTick >= categoryData.spawnTime && !categoryData.doNothing) {
            if (categoryData.gameObj >= 0 && categoryData.gameObj < this.objs.size()) {
                ErosionObjOverlay erosionObjOverlay = (ErosionObjOverlay)this.objs.get(categoryData.gameObj);
                float float0 = categoryData.alpha;
                float float1 = (float)(_eTick - categoryData.spawnTime) / 100.0F;
                if (float1 > 1.0F) {
                    float1 = 1.0F;
                }

                if (float1 < 0.0F) {
                    float1 = 0.0F;
                }

                if (float1 != float0) {
                    IsoObject isoObject0 = null;
                    IsoObject isoObject1 = this.validWall(_sq, true, false);
                    IsoObject isoObject2 = this.validWall(_sq, false, false);
                    if (isoObject1 != null && isoObject2 != null) {
                        isoObject0 = isoObject1;
                    } else if (isoObject1 != null) {
                        isoObject0 = isoObject1;
                    } else if (isoObject2 != null) {
                        isoObject0 = isoObject2;
                    }

                    if (isoObject0 != null) {
                        int int0 = categoryData.curID;
                        byte _byte = 0;
                        int int1 = erosionObjOverlay.setOverlay(isoObject0, int0, _byte, 0, float1);
                        if (int1 >= 0) {
                            categoryData.alpha = float1;
                            categoryData.curID = int1;
                        }
                    } else {
                        categoryData.doNothing = true;
                    }

                    if (categoryData.hasTop != null) {
                        IsoGridSquare isoGridSquare = IsoWorld.instance.CurrentCell.getGridSquare(_sq.getX(), _sq.getY(), _sq.getZ() + 1);
                        if (isoGridSquare != null) {
                            this.update(isoGridSquare, _sqErosionData, categoryData.hasTop, _chunkData, _eTick);
                        }
                    }
                }
            } else {
                categoryData.doNothing = true;
            }
        }
    }

    @Override
    public void init() {
        for (int int0 = 0; int0 < 100; int0++) {
            this.spawnChance[int0] = int0 <= 50 ? 100 : 0;
        }

        String string = "d_wallcracks_1_";
        int[] int1 = new int[]{2, 2, 2, 1, 1, 1, 0, 0, 0};

        for (int int2 = 0; int2 < 3; int2++) {
            this.objsRef.add(new ArrayList());
            this.topRef.add(new ArrayList());
            this.botRef.add(new ArrayList());
        }

        for (int int3 = 0; int3 < int1.length; int3++) {
            for (int int4 = 0; int4 <= 7; int4++) {
                int int5 = int4 * 9 + int3;
                ErosionObjOverlaySprites erosionObjOverlaySprites = new ErosionObjOverlaySprites(1, "WallCracks");
                erosionObjOverlaySprites.setSprite(0, string + int5, 0);
                this.objs.add(new ErosionObjOverlay(erosionObjOverlaySprites, 60, true));
                ((ArrayList)this.objsRef.get(int1[int3])).add(this.objs.size() - 1);
                if (int4 == 0) {
                    ((ArrayList)this.botRef.get(int1[int3])).add(this.objs.size() - 1);
                } else if (int4 == 1) {
                    ((ArrayList)this.topRef.get(int1[int3])).add(this.objs.size() - 1);
                }
            }
        }
    }

    @Override
    protected ErosionCategory.Data allocData() {
        return new WallCracks.CategoryData();
    }

    @Override
    public void getObjectNames(ArrayList<String> list) {
        for (int _int = 0; _int < this.objs.size(); _int++) {
            if (((ErosionObjOverlay)this.objs.get(_int)).name != null && !list.contains(((ErosionObjOverlay)this.objs.get(_int)).name)) {
                list.add(((ErosionObjOverlay)this.objs.get(_int)).name);
            }
        }
    }

    private static final class CategoryData extends ErosionCategory.Data {
        public int gameObj;
        public int spawnTime;
        public int curID = -999999;
        public float alpha;
        public WallCracks.CategoryData hasTop;

        @Override
        public void save(ByteBuffer byteBuffer) {
            super.save(byteBuffer);
            byteBuffer.put((byte)this.gameObj);
            byteBuffer.putShort((short)this.spawnTime);
            byteBuffer.putInt(this.curID);
            byteBuffer.putFloat(this.alpha);
            if (this.hasTop != null) {
                byteBuffer.put((byte)1);
                byteBuffer.put((byte)this.hasTop.gameObj);
                byteBuffer.putShort((short)this.hasTop.spawnTime);
                byteBuffer.putInt(this.hasTop.curID);
                byteBuffer.putFloat(this.hasTop.alpha);
            } else {
                byteBuffer.put((byte)0);
            }
        }

        @Override
        public void load(ByteBuffer byteBuffer, int _int) {
            super.load(byteBuffer, _int);
            this.gameObj = byteBuffer.get();
            this.spawnTime = byteBuffer.getShort();
            this.curID = byteBuffer.getInt();
            this.alpha = byteBuffer.getFloat();
            boolean _boolean = byteBuffer.get() == 1;
            if (_boolean) {
                this.hasTop = new WallCracks.CategoryData();
                this.hasTop.gameObj = byteBuffer.get();
                this.hasTop.spawnTime = byteBuffer.getShort();
                this.hasTop.curID = byteBuffer.getInt();
                this.hasTop.alpha = byteBuffer.getFloat();
            }
        }
    }
}
